package cn.zzf.core;

import cn.zgf.core.BeanListHandler;
import cn.zgf.core.ReadExcel;
import cn.zzf.mapper.*;
import cn.zzf.po.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author GaoFeng2017
 * @date 2018-04-03 21:53:15
 **/

public class ExperimentItemWriteToDb {

    @Test
    public void write() throws IOException {
        ReadExcel readExcel = new ReadExcel("H:\\2017-2018学年第二学期实验安排表-软件移动-0328.xls");
        List<ExperimentItem> list = readExcel.read(1,5,4,new BeanListHandler<>(ExperimentItem.class));
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
                .build(Resources.getResourceAsStream("mybatis.xml"));

            /*dao类*/
        SqlSession sqlSession = sessionFactory.openSession();
        ClassroomInfoMapper classroomInfoMapper = sqlSession.getMapper(ClassroomInfoMapper.class);
        System.out.println(list);
        System.exit(0);
        for (ExperimentItem experimentItem : list) {
            String room = experimentItem.getClassroom();
            ClassroomInfoExample classroomInfoExample = new ClassroomInfoExample();
            ClassroomInfoExample.Criteria criteria1 = classroomInfoExample.createCriteria();
            criteria1.andBuildingEqualTo("实训楼").andNameLike("实-"+room.replace(".0","")+"%");
            List<ClassroomInfo> classroomInfos = classroomInfoMapper.selectByExample(classroomInfoExample);
            long cid = 0;
            if (classroomInfos.isEmpty()) {
                ClassroomInfo c = new ClassroomInfo();
                c.setName("实-"+room.replace(".0",""));
                c.setGmtCreate(new Date());
                c.setCampus((byte) 1);
                c.setBuilding("实训楼");
                c.setDescription("实验课教室");
                classroomInfoMapper.insertSelective(c);
                cid = c.getId();
            } else {
                ClassroomInfo bClassroom = classroomInfos.get(0);
                cid = bClassroom.getId();
                bClassroom.setDescription("实验课教室");
                classroomInfoMapper.updateByPrimaryKey(bClassroom);
            }

            TeacherInfoMapper teacherInfoMapper = sqlSession.getMapper(TeacherInfoMapper.class);
            TeacherInfoExample teacherInfoExample = new TeacherInfoExample();
            TeacherInfoExample.Criteria criteria2 = teacherInfoExample.createCriteria();
            criteria2.andNameEqualTo(experimentItem.getTeacher());
            List<TeacherInfo> teacherInfos = teacherInfoMapper.selectByExample(teacherInfoExample);

            if (teacherInfos.isEmpty()) {
                continue;
            }

            TeacherLessonMapper teacherLessonMapper = sqlSession.getMapper(TeacherLessonMapper.class);
            TeacherLessonExample teacherLessonExample = new TeacherLessonExample();
            TeacherLessonExample.Criteria criteria3 = teacherLessonExample.createCriteria();
            criteria3.andTeacherIdEqualTo(teacherInfos.get(0).getId());
            criteria3.andPeriodEqualTo(experimentItem.getPeriod().replace("、","-"));
            criteria3.andNameEqualTo(experimentItem.getName());
            criteria3.andWeekdayEqualTo((byte) ("一二三四五六日".indexOf(experimentItem.getWeekday()) + 1));
            criteria3.andYearTermIdEqualTo(1L);
            criteria3.andWeekEqualTo(Byte.parseByte(experimentItem.getWeek().replace(".0","")));
            List<TeacherLesson> teacherLessons = teacherLessonMapper.selectByExample(teacherLessonExample);

            if (!teacherLessons.isEmpty()) {
                teacherLessonMapper.deleteByPrimaryKey(teacherLessons.get(0).getId());
            }

            System.out.println(experimentItem);

            String[] cs = parseClass(experimentItem.getClassInfo().trim());

            for (String s : cs) {
                ClassInfoMapper classInfoMapper = sqlSession.getMapper(ClassInfoMapper.class);
                ClassInfoExample classInfoExample = new ClassInfoExample();
                ClassInfoExample.Criteria criteria4 = classInfoExample.createCriteria();
                criteria4.andNameEqualTo(s);
                List<ClassInfo> classInfos = classInfoMapper.selectByExample(classInfoExample);


                ExperimentLessonMapper experimentLessonMapper = sqlSession.getMapper(ExperimentLessonMapper.class);
                ExperimentLesson experimentLesson = new ExperimentLesson();
                experimentLesson.setClassroomId(cid);
                experimentLesson.setContent(experimentItem.getContent());
                experimentLesson.setGmtCreate(new Date());
                experimentLesson.setCampus((byte) 1);
                experimentLesson.setName(experimentItem.getName());
                experimentLesson.setWeekday((byte) ("一二三四五六日".indexOf(experimentItem.getWeekday()) + 1));
                experimentLesson.setWeek(Byte.parseByte(experimentItem.getWeek().replace(".0","")));
                experimentLesson.setPeriod(experimentItem.getPeriod().replace("、","-"));
                experimentLesson.setTeacherId(teacherInfos.get(0).getId());
                experimentLesson.setYearTermId((long) 1);
                experimentLesson.setClassId(classInfos.get(0).getId());
                experimentLessonMapper.insertSelective(experimentLesson);
            }
        }

        sqlSession.commit();
        sqlSession.close();
    }

    public String[] parseClass(String str) {
        str = str.replaceAll("（","(").replaceAll("）",")");
        str = str.replaceAll("\\)","");
        return str.split("\\(");
    }
}
